package com.centit.airplaneinfo_db.dao;

import com.alibaba.fastjson.JSONArray;
import com.centit.airplaneinfo_db.po.DbRecord;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.jdbc.dao.BaseDaoImpl;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import com.centit.support.database.utils.PageDesc;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class DbRecordDao extends BaseDaoImpl<DbRecord, String> {

    @Override
    public Map<String, String> getFilterField() {
        if( filterField == null){
            filterField = new HashMap<String, String>();

            filterField.put("recId" , CodeBook.EQUAL_HQL_ID);
            filterField.put("recordTitle" , CodeBook.LIKE_HQL_ID);
            filterField.put("recordType" , CodeBook.EQUAL_HQL_ID);
            filterField.put("dataState" , CodeBook.EQUAL_HQL_ID);

        }
        return filterField;
    }

    public JSONArray newDbRecordValue(String recordId) {
        String sql = "select a.*,b.A_DJ_BHS,b.A_DJ_HS,b.ZZJ_DJ_BHS,b.ZZJ_DJ_HS from ( " +
                "SELECT AIR_PLANE_TYPE,COUNT(FLIGHT_HOURS) FLIGHT_HOURS,MAX(RECORD_ID) RECORD_ID FROM DB_HBXX " +
                " where record_id='"+recordId+"' " +
                "GROUP BY AIR_PLANE_TYPE) a LEFT JOIN DB_RECORD b ON a.RECORD_ID=b.REC_ID";
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, new HashMap<>());
        JSONArray jsonArray = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this, params.getQuery(), params.getParams(), null);
        if (jsonArray == null) {
            return new JSONArray();
        }
        return jsonArray;
    }


}
